<?php  

declare(ticks = 1);
pcntl_signal(SIGTERM, "signal_handler");
pcntl_signal(SIGINT, "signal_handler");
error_reporting(E_ALL);

/* Get argv 1  */


if (isset($argv[1])){
   $pkt = new Packet();
   $pkt->SetFilter($argv[1]);
} else{
  $pkt = new Packet();
}
 
 
  /*  open pipe  with ngrep */ 
$handle = popen('ngrep -W byline -d eth0 port 514 ', 'r');

$line ="";





    
  /*  infinite loop */    
while (true){
   $read = fread($handle, 2096000); 
     // echo $read;
        if (preg_match('/<[0-9]+>[0-9]+:(.*)/im',$read,$match)){
            
              $line1 = trim($match[1])."\n";  
              $line = str_replace("\r\n",'',$line1);
      
                          
         if (preg_match('@SIP/Msg/ccsipDisplayMsg@i',$line)  ||  preg_match('/Sent:/im',$line )  || preg_match('/Received:/im',$line )  ){
             $pkt->AddID(time().md5(rand())) ;
         }else{
             $pkt->AddPktData($line);
         } 
         
         
          
        }else{
          echo "ERRORE : ". $read;
        
        }
   

   
   


}

pclose($handle);




class Packet {


   private $debug = true;

   function Packet (){
       $this->idx = 0 ;
       $this->filter   =  null ;
       $this->data[$this->idx] = array();
       $this->c = new Colors();
       
       
      $this->color['ua'] = "yellow"  ;
      $this->color['srv'] = "yellow"  ;
      $this->color['to'] = "green"  ;
      $this->color['from'] = "light_cyan"  ;
   }

   function AddID($id){
        if ($this->debug ) { 
              $this->PlotPkt();
        } 

       if ( $this->idx != $id ){

            $this->idx = $id ;
            $this->data = array();
            $this->data[$this->idx] = "" ;                 
        }
        
            
   }
   
   
   function AddPktData ($data){
      if ($data == ".....") $data = ".";
       
       
       if (!@in_array($data,$this->data[$this->idx])  && $data != "" ){
         if (preg_match('/User-Agent/im',$data)){
             $this->data[$this->idx][] = $this->c->getColoredString( $data,$this->color['ua'] );
         
         }elseif (preg_match('/To:/im',$data)){
             $this->data[$this->idx][] = $this->c->getColoredString( $data,$this->color['to'] );
         
         }elseif (preg_match('/From:/im',$data)){
             $this->data[$this->idx][] = $this->c->getColoredString( $data,$this->color['from'] );
         
         }elseif (preg_match('/Server:/im',$data)){
             $this->data[$this->idx][] = $this->c->getColoredString( $data,$this->color['srv'] );
         
         }else{
            $this->data[$this->idx][] = $data ;
         
         }
       
       
         
       } 
       
   }
   
      
   function SetFilter($f){
     if ($this->debug ) echo $this->c->getColoredString( "set filter : ".$f."\n", "red");
     $this->filter = $f;
   }
   
   
   function PlotPkt(){
   
        $this->print_idx = $this->idx; 
         $v = $this->data[$this->idx];
         
         $pkt = @implode("",$this->data[$this->idx]);

        if (!is_null($this->filter) && stripos( $pkt , $this->filter ) ){
          $this->PrintArr($pkt);
          
           
        }elseif(is_null($this->filter)) {
            $this->PrintArr($pkt);
         }
         
     $this->data[$this->idx] = "" ;
   }
   
   function PrintArr($pkt){
      echo "\n\n".date('d/m/Y H:i:s ->')."  ---  start ---\n";
      print_r ($pkt);
      echo "\n  ---  stop ---\n\n";  
   }
}




class Colors {
		private $foreground_colors = array();
		private $background_colors = array();
 
		public function __construct() {
			// Set up shell colors
			$this->foreground_colors['black'] = '0;30';
			$this->foreground_colors['dark_gray'] = '1;30';
			$this->foreground_colors['blue'] = '0;34';
			$this->foreground_colors['light_blue'] = '1;34';
			$this->foreground_colors['green'] = '0;32';
			$this->foreground_colors['light_green'] = '1;32';
			$this->foreground_colors['cyan'] = '0;36';
			$this->foreground_colors['light_cyan'] = '1;36';
			$this->foreground_colors['red'] = '0;31';
			$this->foreground_colors['light_red'] = '1;31';
			$this->foreground_colors['purple'] = '0;35';
			$this->foreground_colors['light_purple'] = '1;35';
			$this->foreground_colors['brown'] = '0;33';
			$this->foreground_colors['yellow'] = '1;33';
			$this->foreground_colors['light_gray'] = '0;37';
			$this->foreground_colors['white'] = '1;37';
 
			$this->background_colors['black'] = '40';
			$this->background_colors['red'] = '41';
			$this->background_colors['green'] = '42';
			$this->background_colors['yellow'] = '43';
			$this->background_colors['blue'] = '44';
			$this->background_colors['magenta'] = '45';
			$this->background_colors['cyan'] = '46';
			$this->background_colors['light_gray'] = '47';
		}
 
		// Returns colored string
		public function getColoredString($string, $foreground_color = null, $background_color = null) {
			$colored_string = "";
 
			// Check if given foreground color found
			if (isset($this->foreground_colors[$foreground_color])) {
				$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
			}
			// Check if given background color found
			if (isset($this->background_colors[$background_color])) {
				$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
			}
 
			// Add string and end coloring
			$colored_string .=  $string . "\033[0m";
 
			return $colored_string;
		}
 
		// Returns all foreground color names
		public function getForegroundColors() {
			return array_keys($this->foreground_colors);
		}
 
		// Returns all background color names
		public function getBackgroundColors() {
			return array_keys($this->background_colors);
		}
	}




function signal_handler($signal) {
     global  $pkt ;
     global $handle;
     $pkt->AddID('close');
     pclose($handle);
     exit;
}
    
     
?> 